package com.affaris.controller;


import com.affaris.common.QueryPageParam;
import com.affaris.common.Result;
import com.affaris.entity.Apply;
import com.affaris.entity.Users;
import com.affaris.service.ApplyService;
import com.affaris.service.UsersService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lucas
 * @since 2023-05-23
 */

@RestController
@RequestMapping("/apply")
public class ApplyController {

    @Autowired
    private ApplyService applyService;
    @Autowired
    private UsersService usersService;

    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Apply apply) {
        return applyService.save(apply) ? Result.suc() : Result.fail();
    }

    //删除
    @GetMapping("/delete")
    public Result delete(@RequestParam String id) {

        return applyService.removeById(id) ? Result.suc() : Result.fail();
    }

    //更新
    @PostMapping("/update")
    public Result update(@RequestBody Apply apply) {

        boolean applyUpdated = applyService.updateById(apply);
        if (!applyUpdated) {
            return Result.fail();
        }
        // 构建更新条件
        UpdateWrapper<Users> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("student_id", apply.getStudentId())
                .set("user_identity", apply.getUserIdentity() + 1);
        boolean usersUpdated = usersService.update(updateWrapper);

        return usersUpdated ? Result.suc() : Result.fail();
    }

    //拒绝通过
    @PostMapping("/refuse")
    public Result refuse(@RequestBody Apply apply) {
        return applyService.updateById(apply) ? Result.suc() : Result.fail();
    }

    //查询
    @PostMapping("/page")
    public Result page(@RequestBody QueryPageParam queryPageParam) {
        HashMap param = queryPageParam.getParam();
        String name = (String) param.get("name");
        String userIdentity = (String) param.get("userIdentity");
        String organization = (String) param.get("organization");

        Page<Apply> page = new Page();
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());

        LambdaQueryWrapper<Apply> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotBlank(name) && !"null".equals(name)) {
            lambdaQueryWrapper.like(Apply::getName, name);
        }
        if (StringUtils.isNotBlank(userIdentity) && !"null".equals(userIdentity)) {
            lambdaQueryWrapper.eq(Apply::getUserIdentity, userIdentity);
        }
        if (StringUtils.isNotBlank(organization) && !"null".equals(organization)) {
            lambdaQueryWrapper.eq(Apply::getOrganization, organization);
        }

        IPage result = applyService.page(page, lambdaQueryWrapper);
        return Result.suc(result.getRecords(), result.getTotal());
    }


    //查询
    @PostMapping("/myPage")
    public Result myPage(@RequestBody QueryPageParam queryPageParam) {
        HashMap param = queryPageParam.getParam();
        String studentId = (String) param.get("studentId");
        String userIdentity = (String) param.get("userIdentity");

        Page<Apply> page = new Page();
        page.setCurrent(queryPageParam.getPageNum());
        page.setSize(queryPageParam.getPageSize());

        LambdaQueryWrapper<Apply> lambdaQueryWrapper = new LambdaQueryWrapper();
        if (StringUtils.isNotBlank(studentId) && !"null".equals(studentId)) {
            lambdaQueryWrapper.eq(Apply::getStudentId, studentId);
        }
        if (StringUtils.isNotBlank(userIdentity) && !"null".equals(userIdentity)) {
            lambdaQueryWrapper.eq(Apply::getUserIdentity, userIdentity);
        }

        IPage result = applyService.page(page, lambdaQueryWrapper);
        return Result.suc(result.getRecords(), result.getTotal());
    }

    @GetMapping("/list")
    public Result list() {
        List list = applyService.list();
        return Result.suc(list);
    }

    //重复申请验证
    @PostMapping("/duplicate")
    public Result duplicate(@RequestBody Apply apply) {
        LambdaQueryWrapper<Apply> lambdaQueryWrapper = new LambdaQueryWrapper<Apply>()
                .eq(Apply::getStudentId, apply.getStudentId())
                .eq(Apply::getUserIdentity, apply.getUserIdentity())
                .eq(Apply::getStatu,apply.getStatu());
        Apply result = applyService.getOne(lambdaQueryWrapper);
        if (result != null) {
            return Result.fail();
        } else {
            return Result.suc();
        }
    }
}
